/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.plugins.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.dao.plugins.VoteRecordDao;
import org.grayrabbit.cms.entity.VoteRecord;
import org.grayrabbit.cms.vo.plugins.VoteRecordBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>VoteRecord</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 16:08:28 
 */
@Repository
public class VoteRecordDaoImpl extends GenericHibernateDao<VoteRecord, String>
		implements VoteRecordDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From VoteRecord as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see VoteRecordDao#assembly(VoteRecordBean)
	 */
	public QueryCondition assembly(VoteRecordBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see VoteRecordDao#assemblyCount(VoteRecordBean)
	 */
	public QueryCondition assemblyCount(VoteRecordBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see VoteRecordDao#count(VoteRecordBean)
	 */
	public int count(VoteRecordBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see VoteRecordDao#assemblyNameQuery(VoteRecordBean)
	 */
	public QueryCondition assermblyNameQuery(VoteRecordBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see VoteRecordDao#assemblyNameQueryCount(VoteRecordBean)
	 */
	public QueryCondition assemblyNameQueryCount(VoteRecordBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see VoteRecordDao#findByPage(Page, VoteRecordBean)
	 */
	public List<VoteRecord> findByPage(Page<VoteRecord> page,
			VoteRecordBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void transNameQuery(VoteRecordBean bean,QueryCondition qc){
		    if(bean.getVote() != null && StringUtils.isNotBlank(bean.getVote().getVoteId())){
			   qc.add(" And obj.vote.voteId = ?", bean.getVote().getVoteId());
			}
            
	}
	/**
	 * 得到投票数
	 * @return
	 */
	public List<VoteRecord> findByVoteId(String voteId){
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.vote.voteId = ?", voteId);
		return findByQc(qc);
	}

	/**
	 * 得到个投票项投票数
	 * @return
	 */
	public List<VoteRecord> findByOptId(String optId){
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.voteOption.voteOptionId = ?", optId);
		return findByQc(qc);
	}
}